<template>
  <div>
    <h1>点击上传</h1><!--  -->
    <el-upload
      class="upload-demo"
      action="https://jsonplaceholder.typicode.com/posts/"
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      :before-remove="beforeRemove"
      multiple
      :limit="3"
      :on-exceed="handleExceed"
      :file-list="fileList">
      <el-button size="small" type="primary">点击上传</el-button>
      <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
    </el-upload>

    <h1>照片墙</h1>
    <el-upload
      multiple
      action="https://jsonplaceholder.typicode.com/posts/"
      list-type="picture-card"
      :on-preview="handlePictureCardPreview1"
      :on-remove="handleRemove1">
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
    
    <h1>图片列表缩略图</h1>
    <el-upload
      class="upload-demo"
      action="https://jsonplaceholder.typicode.com/posts/"
      :on-preview="handlePreview2"
      :on-remove="handleRemove2"
      :file-list="fileList"
      list-type="picture">
      <el-button size="small" type="primary">点击上传</el-button>
      <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
    </el-upload>
  </div>
</template>

<script>
  export default{
    name: "Uploadd",
    data(){
      return{
        fileList: [
          {name: 'food.jpeg',
  url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'},
          {name: 'food2.jpeg',
 url: 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1517584427,1493751405&fm=26&gp=0.jpg'},
          ],
        dialogImageUrl: '',
        dialogVisible: false
      }
    },
    methods:{
      handleRemove(file, fileList) {/* 文件列表移除文件时的钩子 */
          /*file是被删除的文件信息，fileList未被删除的所有文件信息 */
          console.log(file, fileList);
      },
      handlePreview(file) {/* 点击文件列表中已上传的文件时的钩子 */
          console.log(file);
      },
      handleExceed(files, fileList) {/* 文件超出个数限制时的钩子 */
          this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
      },
      /* 删除文件之前的钩子，参数为上传的文件和文件列表，
      若返回 false 或者返回 Promise 且被 reject，则停止删除。*/
      beforeRemove(file, fileList) {
         // this.$confirm(`确定移除 ${ file.name }？`);
          alert("确定删除吗")
          if(fileList.length<=2){
            alert("文件不能少于2个")
            return false;
          }
          return true;
      },
      handleRemove1(file, fileList) {
          console.log(file, fileList);
      },
      handlePictureCardPreview1(file) {
        console.log("文件被点击")
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      },
      handleRemove2(file, fileList) {
          console.log(file, fileList);
      },
      handlePreview2(file) {
          console.log(file);
      }
    }
  }
</script>

<style>
</style>
